/*main script, manages all canvas.*/

/*********************************************************************************************************************/
/*GLOBAL VARIABLES****************************************************************************************************/
/*********************************************************************************************************************/
//maps array to place tiles, overtiles, monsters, buildings (Check XML)
var tile;
var tileOver;
var monster;
var building;

//main variables :)
var displayer;
var game;
var menuEditor;
var mapEditor;
var resourceManager;

//main variables, context and canvas
var ctx;
var canvas;

// --- map edition ---
var mapEditing = true;

//select box for map editing
var tileSelect;

// --- Drawing variables ---

//Images after loading
var tileImg = new Array();
var tileOverImg = new Array();
var monsterImg = new Array();
var buildingImg = new Array();

//depends on tile size but I don't know how >< probably image width-2px for semi-alpha border.
var tileUnit = 50;

// difference between canvas border and map border. depends on number of tiles and tileUnit
var mapX;
var mapY;

//coods of tile where mouse is
var ymouse;
var xmouse;

// --- init ---

function init(){
	/*function that initialize the game*/

	game = new Game();
	mapEditor = new MapEditor();
	resourceManager = new ResourceManager();
	displayer = new Displayer();
	menuEditor = new MenuEditor();

	//get the select box for map editing
	if (mapEditing) {
		tileSelect = document.getElementById('tile');
	}
	
	//get the canvas and its context
	canvas = document.getElementById('main');
	ctx = canvas.getContext('2d');
	
	//load xml
	var XML = new XMLManager();
	XML.loadXML("xml/map1.xml");
	tile = XML.getContent('tiles');
	tileOver = XML.getContent('overtiles');
	monster = XML.getContent('monsters');
	building = XML.getContent('buildings');
	
	// Set the difference between canvas border and map border. depends on number of tiles and tileUnit
	mapX = 40+tile[0].length*tileUnit*1.17; //tileUnit*1.17 is some kind of f(tileUnit,sin15,sin75,sin45,sin13.5) that I didn't calculate
	mapY = 40;
	
	//add event listener for mouse clics on map
	canvas.addEventListener('mousemove', game.mouseCheck, false);
	canvas.addEventListener("click", mapEditor.mouseClick, false);
	
	//start loading pics and start the game
	resourceManager.loadImg();
}


